<template>
  <!-- v-if解决页面闪烁 -->
  <view v-if="goods_info.goods_name">
    <!-- 轮播图区 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item1,index1) in goods_info.pics " :key="index1" @click="preview(index1)">
        <image :src="item1.pics_big" mode=""></image>
      </swiper-item>
    </swiper>
    <!-- 商品描述区 -->
    <view class="datail-box">
      <view class="price">￥{{goods_info.goods_price}}</view>
      <view class="descrip">
        <view class="left-dsc">{{goods_info.goods_name}}</view>
        <view class="right-dec">
          <uni-icons type="star" size="24"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="yunfei">快递：免运费</view>
      <!-- 渲染html属性 -->
      <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    </view>
      <!-- 商品导航组件 -->
      <view class="goods_nav">
        <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
          @buttonClick="buttonClick" />
      </view>
 
    </view>
</template>

<script>
  import { mapState,mapMutations,mapGetters } from 'vuex'
  export default {
    computed: {
      ...mapState('my_cart',[]),
      ...mapGetters ('my_cart',['total'])
    },
    watch: {
      // total (newVal) {
      //   const result = this.options.find((x) => x.text === '购物车')
      //   if (result) {
      //     result.info =newVal
      //   }
      // }
      total: {
        handler(newVal) {
          const res = this.options.find(x => x.text === '购物车')
          if (res) {
            res.info = newVal
          }
        },
        // immediate 属性表示该侦听器，是否在页面初次加载完毕后立即调用
        immediate: true,
      }
    },
    data() {
      return {
        goods_info: [],
        options: [{
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    onLoad(option) {
      const goods_id = option.goods_id
      this.getGoodsInfo(goods_id)
    },
    methods: {
      ...mapMutations('my_cart', ['addToCart']),
      onClick(e){
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      buttonClick(e) {
        if (e.content.text === '加入购物车') {
      // 2. 组织一个商品的信息对象
      const goods = {
         goods_id: this.goods_info.goods_id,       // 商品的Id
         goods_name: this.goods_info.goods_name,   // 商品的名称
         goods_price: this.goods_info.goods_price, // 商品的价格
         goods_count: 1,                           // 商品的数量
         goods_small_logo: this.goods_info.goods_small_logo, // 商品的图片
         goods_state: true                         // 商品的勾选状态
      }
          this.addToCart(goods)
        }
      },
      preview(index1) {
        uni.previewImage({
          current: index1,
          urls: this.goods_info.pics.map(x => x.pics_big)
        })
      },
      async getGoodsInfo(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        if (res.meta.status !== 200) return uni.$showMsg('数据请求失败')
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style= "display:block;"')
          .replace(/webp/g, 'jpg')
        this.goods_info = res.message
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    swiper-item {
      image {
        width: 100%;
        height: 100%;
      }
    }
  }

  .datail-box {
    margin: 5px 5px;
    font-size: 12px;

    .price {
      color: #ff0000;
      font-size: 16px;
    }

    .descrip {
      display: flex;

      .left-dsc {
        padding-right: 10px;
      }

      .right-dec {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-around;
        border-left: 1px solid #C0C4CC;
        padding-left: 2px;
      }
    }

    .yunfei {
      color: #8F939C;

    }
  }
  .goods_nav {
    position: fixed;
    width: 100%;
    bottom: 0;
    left: 0;
  }
</style>
